<?php
// leetcode 530题，获取二叉搜索树相邻两个节点的值的最小差值
/**
 * Definition for a binary tree node.
 */
 class TreeNode {
     public $val = null;
     public $left = null;
     public $right = null;
     function __construct($value) { $this->val = $value; }
 }

class Solution {

     protected $arr = [];
    /**
     * @param TreeNode $root
     * @return Integer
     */
    function getMinimumDifference($root) {
        $this->traversal($root);
        if (count($this->arr) < 2) {
            return 0;
        }
        $res = PHP_INT_MIN;
        for ($i = 1; $i < count($this->arr); $i++) {
            $diff = $this->arr[$i] - $this->arr[$i - 1];
            $res = max($res, $diff);
        }
        return $res;
    }

    function traversal($root) {
        if ($root == null) return;
        $this->traversal($root->left);
        $this->arr[] = $root->val;
        $this->traversal($root->right);
    }
}
